﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerBase.Other;
using ServerPublic.Data;
using ServerPublic.Manager;
using ServerGame.GameObject;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;

namespace ServerGame.Manager
{
    /// <summary>
    /// 管理 公告
    /// </summary>
    public static class NoticeMgr
    {
        public class DataNoticeRolling
        {
            public int Id;
            public DateTime DtShow = new DateTime();//播放时间
            public bool State = false;//是否播放
        }
        public static Dictionary<int, DataNoticeRolling> DictNoticeRolling = new Dictionary<int, DataNoticeRolling>();

        public static void DayUpdateDayZeroOClock()
        {
            ResetConfNoticeRolling();
        }
        public static void LoopMinute()
        {
            TryPushConfNoticeRolling();
        }
        // 定时走马灯 服务器启动或者每日更新时重置
        public static void ResetConfNoticeRolling()
        {
            loger.Debug("NoticeManager.ResetConfNoticeRolling");
            var dtNow = DateTime.Now;
            DictNoticeRolling.Clear();
            foreach (var conf in ConfNoticeRolling.Values)
            {
                if (conf.Weak.Contains((int)(dtNow.DayOfWeek)))
                {
                    var item = new DataNoticeRolling()
                    {
                        Id = conf.Id,
                        DtShow = dtNow.Date.AddHours(conf.Time[0]).AddMinutes(conf.Time[1]),
                        State = false,
                    };
                    if (dtNow < item.DtShow)
                    {
                        DictNoticeRolling.Add(item.Id, item);
                    }
                }
            }
        }
        // 定时走马灯 每分钟检查一次
        public static void TryPushConfNoticeRolling()
        {
            //loger.Debug("NoticeManager.TryPushConfNoticeRolling");
            var dtNow = DateTime.Now;

            //系统的
            foreach (var item in DictNoticeRolling.Values)
            {
                //loger.Debug($"Id{item.Id} DtShow{item.DtShow} State{item.State}");
                if ((!item.State) && item.DtShow <= dtNow)
                {
                    if (ConfNoticeRolling.TryGetValue(item.Id, out var conf))
                    {
                        NoticeRollingToPlayer(conf.Text.TextLanguage(), conf.Num, ENoticeRollingType.公告走马灯);
                    }
                    item.State = true;
                    break;
                }
            }

            //GM的
            List<long> 播放列表 = new List<long>();
            foreach (var kvp in DictNoticeRollingGm)
            {
                var GtArgs = kvp.Value;
                if (GtArgs.TimeSend.ToDateTime() <= dtNow)
                {
                    播放列表.Add(kvp.Key);
                    GmNoticeRollingSend(GtArgs);
                }
            }
            播放列表.ForEach(it=> DictNoticeRollingGm.Remove(it));
            //loger.Debug($"DictNoticeRollingGm {DictNoticeRollingGm.Count}");
        }
        //GM定时走马灯列表
        public static Dictionary<long, CLS_GtNoticeRolling> DictNoticeRollingGm = new Dictionary<long, CLS_GtNoticeRolling>();
        // GM发送走马灯
        public static void GmNoticeRolling(CLS_GtNoticeRolling GtArgs)
        {
            var dtNow = DateTime.Now;
            if (GtArgs.TimeSend.ToDateTime() <= dtNow)
            {
                GmNoticeRollingSend(GtArgs);
            }
            else
            {
                DictNoticeRollingGm[GtArgs.Uid] = GtArgs;
            }
        }
        public static void GmNoticeRollingSend(CLS_GtNoticeRolling GtArgs)
        {
            NoticeRollingToPlayer(GtArgs.Text, GtArgs.Count, ENoticeRollingType.临时走马灯);
            loger.Warn($"GmNoticeRollingSend {GtArgs}");
        }

        
        //推送走马灯
        public static void PushNoticeRolling(Player player)
        {
            if (player.ListNoticeRolling.Count > 0)
            {
                var msg = new G2C_Notice_Rolling()
                {
                    ListNoticeRolling = new List<CLS_NoticeRolling>(player.ListNoticeRolling)
                };
                player.Send(msg);
                player.ListNoticeRolling.Clear();
            }
        }
        // 给玩家发送走马灯
        public static void NoticeRollingToPlayer(string text, int count = 1, ENoticeRollingType RollingType = ENoticeRollingType.玩家走马灯)
        {
            //转发到游戏服务器
            var Msg = new CLS_NoticeRolling()
            {
                Type = (byte)RollingType,
                Text = text,
                Count = count,
            };
            foreach (var item in DataGame.DictPlayerOnline.Values)
            {
                item.ListNoticeRolling.Add(Msg);
            }
        }
    }
    public partial class DispatcherMsg
    {
        [ApiDpPro((short)EProtocolId.C2G_NOTICE_SYSTEM, "系统公告")]
        public short OnNoticeSystem()
        {
            (var Req, var Rsp) = ThisReq.Body.OnNoticeSystem();

            Rsp.Text = PubDataAllMgr.NoticeSystem;

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_NOTICE_ACTIVITY, "活动公告")]
        public short OnNoticeActivity()
        {
            (var Req, var Rsp) = ThisReq.Body.OnNoticeActivity();

            var Text = PubDataAllMgr.NoticeActivity;
            Rsp.ListText = Text.ToListString(UtilEnum.分隔符竖线);

            return Send(Rsp);
        }
    }
}